﻿CREATE PROCEDURE [dbo].[yaf_forum_save](
                @ForumID      INT,
                @CategoryID   INT,
                @ParentID     INT  = NULL,
                @Name         NVARCHAR(128),
                @Description  NVARCHAR(255),
                @SortOrder    SMALLINT,
                @Locked       BIT,
                @Hidden       BIT,
                @IsTest       BIT,
                @Moderated    BIT,
                @RemoteURL    NVARCHAR(100)  = NULL,
                @ThemeURL     NVARCHAR(100)  = NULL,
                @AccessMaskID INT  = NULL)
AS
    BEGIN
        DECLARE  @BoardID INT
        DECLARE  @Flags INT
        SET @Flags = 0
        IF @Locked <> 0
        SET @Flags = @Flags | 1
        IF @Hidden <> 0
        SET @Flags = @Flags | 2
        IF @IsTest <> 0
        SET @Flags = @Flags | 4
        IF @Moderated <> 0
        SET @Flags = @Flags | 8
        IF @ForumID > 0
        BEGIN
            UPDATE yaf_Forum
            SET    ParentID = @ParentID,
                   Name = @Name,
                   Description = @Description,
                   SortOrder = @SortOrder,
                   CategoryID = @CategoryID,
                   RemoteURL = @RemoteURL,
                   ThemeURL = @ThemeURL,
                   Flags = @Flags
            WHERE  ForumID = @ForumID
        END
        ELSE
        BEGIN
            SELECT @BoardID = BoardID
            FROM   yaf_Category
            WHERE  CategoryID = @CategoryID
            INSERT INTO yaf_Forum
                       (ParentID,
                        Name,
                        Description,
                        SortOrder,
                        CategoryID,
                        NumTopics,
                        NumPosts,
                        RemoteURL,
                        ThemeURL,
                        Flags)
            VALUES     (@ParentID,
                        @Name,
                        @Description,
                        @SortOrder,
                        @CategoryID,
                        0,
                        0,
                        @RemoteURL,
                        @ThemeURL,
                        @Flags)
            SELECT @ForumID = Scope_identity()
            INSERT INTO yaf_ForumAccess
                       (GroupID,
                        ForumID,
                        AccessMaskID)
            SELECT GroupID,
                   @ForumID,
                   @AccessMaskID
            FROM   yaf_Group
            WHERE  BoardID = @BoardID
        END
        SELECT ForumID = @ForumID
    END


